<?php

namespace backend\controllers;

use Yii;
use backend\models\JjrProjectHouse;
use backend\models\JjrProject;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * JjrProjectHouseController implements the CRUD actions for JjrProjectHouse model.
 */
class JjrProjectHouseController extends BaseController
{
    public $enableCsrfValidation = false;
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all JjrProjectHouse models.
     * @return mixed
     */
    public function actionIndex($id="")
    {

        $query = JjrProjectHouse::find()->alias('ph')
            ->orderBy(['ph.create_time'=>SORT_DESC])
            ->leftJoin('yii2_jjr_project p','ph.jjr_project_id=p.id')
            ->select("ph.*,p.name project_name")->asArray();
        if($id){
            $query->where(['p.id'=>$id]);
        }
        $dataProvider = new ActiveDataProvider([
            'query' => $query
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single JjrProjectHouse model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $house = JjrProjectHouse::find()->alias('ph')
            ->leftJoin('yii2_jjr_project p','ph.jjr_project_id=p.id')->where(['ph.id'=>$id])
            ->select("ph.*,p.name project_name")->asArray()->one();
        return $this->render('view', [
            'model' => $house,
        ]);
    }

    /**
     * Creates a new JjrProjectHouse model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new JjrProjectHouse();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing JjrProjectHouse model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $project = JjrProject::findOne($model->jjr_project_id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
            'project'=>$project
        ]);
    }

    /**
     * Deletes an existing JjrProjectHouse model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the JjrProjectHouse model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return JjrProjectHouse the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = JjrProjectHouse::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
